/* Vecmath.

   Copyright (C) 2001, 2002, 2003 Stefan Maierhofer.

   Written by Stefan Maierhofer <sm@cg.tuwien.ac.at>

   This file is part of Vecmath.

   Vecmath is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   Vecmath is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Vecmath; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */

using System;

namespace Vecmath
{
    /// <summary>
    /// Pnt3F.
    /// </summary>
    public struct Pnt3F
    {

        public static Pnt3F Identity = new Pnt3F(0,0,0);

        public Pnt3F(float xyz)
        {
            _x = new float[3];
            _x[0] = xyz; _x[1] = xyz; _x[2] = xyz;
        }

        public Pnt3F(float x, float y, float z)
        {
            _x = new float[3];
            _x[0] = x;
            _x[1] = y;
            _x[2] = z;
        }

        public Pnt3F(ref float [] x)
        {
            _x = new float[3];
            _x[0] = x[0]; _x[1] = x[1]; _x[2] = x[2];
        }

        public float this [int index]
        {
            get { return _x[index]; }
            set { _x[index] = value; }
        }

        public float x
        {
            get { return _x[0]; }
            set { _x[0] = value; }
        }

        public float y
        {
            get { return _x[1]; }
            set { _x[1] = value; }
        }

        public float z
        {
            get { return _x[2]; }
            set { _x[2] = value; }
        }

        public static explicit operator Pnt3F(Pnt3D v)
        {
            return new Pnt3F((float)v.x, (float)v.y, (float)v.z);
        }

        public static explicit operator Pnt3F(Vec3F v)
        {
            return new Pnt3F(v.x, v.y, v.z);
        }

        public static Pnt3F operator+(Pnt3F v, float f)
        {
            return new Pnt3F(v[0] + f, v[1] + f, v[2] + f);
        }

        public static Pnt3F operator+(Pnt3F v, Pnt3F w)
        {
            return new Pnt3F(v[0] + w[0], v[1] + w[1], v[2] + w[2]);
        }

        public static Pnt3F operator+(Pnt3F v, Vec3F w)
        {
            return new Pnt3F(v[0] + w[0], v[1] + w[1], v[2] + w[2]);
        }

        public static Pnt3F operator-(Pnt3F v)
        {
            return new Pnt3F(-v[0], -v[1], -v[2]);
        }

        public static Pnt3F operator-(Pnt3F v, float f)
        {
            return new Pnt3F(v[0] - f, v[1] - f, v[2] - f);
        }

        public static Vec3F operator-(Pnt3F v, Pnt3F w)
        {
            return new Vec3F(v[0] - w[0], v[1] - w[1], v[2] - w[2]);
        }

        public static Pnt3F operator-(Pnt3F v, Vec3F w)
        {
            return new Pnt3F(v[0] - w[0], v[1] - w[1], v[2] - w[2]);
        }

        public static Pnt3F operator*(Pnt3F v, float f)
        {
            return new Pnt3F(v[0] * f, v[1] * f, v[2] * f);
        }

        public static Pnt3F operator/(Pnt3F v, float f)
        {
            return new Pnt3F(v[0] / f, v[1] / f, v[2] / f);
        }

        public int MinDim
        {
            get
            {
                return (_x[0] < _x[1])
                    ? ((_x[0] < _x[2]) ? 0 : 2)
                    : ((_x[1] < _x[2]) ? 1 : 2);
            }
        }

        public int MaxDim
        {
            get
            {
                return (_x[0] > _x[1])
                    ? ((_x[0] > _x[2]) ? 0 : 2)
                    : ((_x[1] > _x[2]) ? 1 : 2);
            }
        }

        public float Minimum
        {
            get
            {
                return (_x[0] < _x[1])
                    ? ((_x[0] < _x[2]) ? _x[0]
                    : _x[2]) : ((_x[1] < _x[2]) ? _x[1] : _x[2]);
            }
        }

        public float Maximum
        {
            get
            {
                return (_x[0] > _x[1])
                    ? ((_x[0] > _x[2]) ? _x[0]
                    : _x[2]) : ((_x[1] > _x[2]) ? _x[1] : _x[2]);
            }
        }

        public static bool operator<(Pnt3F a, Pnt3F b)
        {
            return (
                a._x[0] < b._x[0] &&
                a._x[1] < b._x[1] &&
                a._x[2] < b._x[2]
                );
        }

        public static bool operator<=(Pnt3F a, Pnt3F b)
        {
            return (
                a._x[0] <= b._x[0] &&
                a._x[1] <= b._x[1] &&
                a._x[2] <= b._x[2]
                );
        }

        public static bool operator==(Pnt3F a, Pnt3F b)
        {
            return (
                a._x[0] == b._x[0] &&
                a._x[1] == b._x[1] &&
                a._x[2] == b._x[2]
                );
        }

        public static bool operator!=(Pnt3F a, Pnt3F b)
        {
            return (
                a._x[0] != b._x[0] ||
                a._x[1] != b._x[1] ||
                a._x[2] != b._x[2]
                );
        }

        public static bool operator>=(Pnt3F a, Pnt3F b)
        {
            return (
                a._x[0] >= b._x[0] &&
                a._x[1] >= b._x[1] &&
                a._x[2] >= b._x[2]
                );
        }

        public static bool operator>(Pnt3F a, Pnt3F b)
        {
            return (
                a._x[0] > b._x[0] &&
                a._x[1] > b._x[1] &&
                a._x[2] > b._x[2]
                );
        }

        public static Pnt3F Min(Pnt3F a, Pnt3F b)
        {
            return new Pnt3F(
                Math.Min(a._x[0], b._x[0]),
                Math.Min(a._x[1], b._x[1]),
                Math.Min(a._x[2], b._x[2])
                );
        }

        public static Pnt3F Max(Pnt3F a, Pnt3F b)
        {
            return new Pnt3F(
                Math.Max(a._x[0], b._x[0]),
                Math.Max(a._x[1], b._x[1]),
                Math.Max(a._x[2], b._x[2])
                );
        }

        internal float [] _x;

        public override bool Equals(object obj)
        {
            Pnt3F x = (Pnt3F)obj;
            return (
                _x[0] == x._x[0] &&
                _x[1] == x._x[1] &&
                _x[2] == x._x[2]
                );
        }

        public override int GetHashCode()
        {
            return base.GetHashCode ();
        }

        public override string ToString()
        {
            return "Pnt3F(" + _x[0] + ", " + _x[1] + ", " + _x[2] + ")";
        }

    }

}
